/*
 * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.mrd.base;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mrd.framework.ui.filter.Filter;
import com.mrd.framework.ui.filter.SearchRule;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dengdc on 2017/8/1.
 */
public class Jqgrid<T> extends com.mrd.framework.ui.Jqgrid {
    /**
     * 获取过滤条件map
     */
    public Map<String, Object> getFilterMap() {
        Map<String, Object> filterMap = new HashMap<>();
        Filter filter = this.getFilters();
        if (filter != null) {
            List<SearchRule> searchRuleList = filter.getRules();
            if (CollectionUtils.isNotEmpty(searchRuleList)) {
                for (SearchRule searchRule : searchRuleList) {
                    filterMap.put(searchRule.getField(), searchRule.getData());
                }
            }
        }
        return filterMap;
    }

    /**
     * mybatis的排序
     */
    public void sortWithMybatis() {
        Sort.Order order = this.getOrder();
        if (order != null) {
            String direction = order.getDirection().toString();
            String propertyName = order.getProperty();
            PageHelper.orderBy(propertyName + " " + direction);
        }
    }

    /**
     * mybatis分页
     */
    public void pageWithMybatis() {
        Pageable page = this.getPage();
        int pageNumber = page.getPageNumber();
        int pageSize = page.getPageSize();
        //分页
        PageHelper.startPage(pageNumber + 1, pageSize);
    }

    public Page<T> pageResult(List<T> list) {
        PageInfo<T> pageInfo = new PageInfo<>(list);
        return new PageImpl(list, this.getPage(), pageInfo.getTotal());
    }

    /**
     * 组装mybatis查询条件
     *
     * @return "and *"
     */
    public String getWhereCase() {
        Filter filter = this.getFilters();
        StringBuffer whereCase = new StringBuffer();
        if (filter != null) {
            List<SearchRule> searchRuleList = filter.getRules();
            if (CollectionUtils.isNotEmpty(searchRuleList)) {
                for (SearchRule searchRule : searchRuleList) {
                    String op = "";
                    whereCase.append(" and " + searchRule.getField());
                    switch (searchRule.getOp()) {
                        case eq:
                            op = "=";
                            break;
                        case ne:
                            op = "<>";
                            break;
                        case lt:
                            op = "<";
                            break;
                        case le:
                            op = "<=";
                            break;
                        case gt:
                            op = ">";
                            break;
                        case ge:
                            op = ">=";
                            break;
                        case ew:
                            op = " LIKE ";
                            break;
                        case cn:
                            op = " LIKE ";
                            break;
                        case bw:
                            op = " LIKE ";
                            break;
                        case en:
                            op = " NOT LIKE ";
                            break;
                        case nc:
                            op = " NOT LIKE ";
                            break;
                        case bn:
                            op = " NOT LIKE ";
                            break;
                        case in:
                            op = " IN ";
                            break;
                        case ni:
                            op = " NOT IN ";
                            break;
                        case inl:
                            op = " is null ";
                            break;
                        case nnl:
                            op = " is not null ";
                            break;
                        default:
                            op = "=";
                            break;
                    }
                    whereCase.append(op);
                    String value = (String) searchRule.getData();
                    value = value.trim();
                    if (value.matches("\\d{4}-\\d{2}-\\d{2}")) {
                        whereCase.append("to_date('" + value + "','yyyy-mm-dd')");
                        if ("<=".equals(op)) {
                            whereCase.append("+1");
                        }
                    } else if (op.contains("LIKE")) {
                        whereCase.append("'%" + value + "%'");
                    } else if (op.contains("IN")) {
                        whereCase.append("('" + value.replaceAll(",", "','") + "')");
                    } else if (op.contains("null")) {

                    } else {
                        whereCase.append("'" + value + "'");
                    }
                }
            }
            if (whereCase.length() > 5) {
                whereCase.replace(0, 4, " where ");
            }
        }
        return whereCase.toString();
    }
}
